#include <typeConvert.h>

static int checkSystemBigEndian()
{
    union
    {
        uint8_t a;
        uint16_t b;
    } checkval;
    checkval.b = 0x0102;
    return (checkval.a == 0x01);
}

void typeOverturnCommon(void *valuepointer, int valuesize)
{
    char *tmppointer = (char *)valuepointer;
    if (!valuepointer)
    {
        logerror("");
        return;
    }
    if (valuesize % 2)
    {
        logerror("%d", valuesize);
        return;
    }
    for (int i = 0; i < valuesize / 2; i++)
    {
        SWAP_BASE_VAL(tmppointer[i], tmppointer[valuesize - 1 - i]);
    }
}

void typeOverturnShort(short *value)
{
    typeOverturnCommon(value, sizeof(short));
}
void bigEndianConvertShort(short *value) // if system is big, overturn value
{
    if (checkSystemBigEndian())
    {
        typeOverturnShort(value);
    }
}

void typeOverturnInt(int *value)
{
    typeOverturnCommon(value, sizeof(int));
}
void bigEndianConvertInt(int *value) // if system is big, overturn value
{
    if (checkSystemBigEndian())
    {
        typeOverturnInt(value);
    }
}

void typeOverturnInt64(uint64_t *value)
{
    typeOverturnCommon(value, sizeof(uint64_t));
}
void bigEndianConvertInt64(uint64_t *value) // if system is big, overturn value
{
    if (checkSystemBigEndian())
    {
        typeOverturnInt64(value);
    }
}

void typeOverturnFloat(float *value)
{
    typeOverturnCommon(value, sizeof(float));
}
void bigEndianConvertFloat(float *value) // if system is big, overturn value
{
    if (checkSystemBigEndian())
    {
        typeOverturnFloat(value);
    }
}

void typeOverturnDouble(double *value)
{
    typeOverturnCommon(value, sizeof(double));
}
void bigEndianConvertDouble(double *value) // if system is big, overturn value
{
    if (checkSystemBigEndian())
    {
        typeOverturnDouble(value);
    }
}


